package com.lungle.warmdiary.app.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lungle.warmdiary.R;

/**
 * Created by God on 2016/8/16.
 */
public class InputIArrowView extends LinearLayout implements TextWatcher {
    private TextView mErrorTip;
    private TextView mEtInput;
    private ImageView mIvTip;

    public InputIArrowView(Context context) {
        super(context);

        initView(context);
    }


    public InputIArrowView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputArrowView);
        initWidget(ta);

    }

    private void initWidget(TypedArray ta) {

        mEtInput = (TextView) findViewById(R.id.et_input);
        mIvTip = (ImageView) findViewById(R.id.iv_tip);

        mErrorTip = (TextView) findViewById(R.id.tv_error_tip);


        String text = ta.getString(R.styleable.InputArrowView_text);
        String hint = ta.getString(R.styleable.InputArrowView_hint);

        float textSize = ta.getDimension(R.styleable.InputArrowView_textSize, 14);
        mEtInput.setTextSize(textSize);
        mErrorTip.setTextSize(textSize);


        mEtInput.setHint(hint);
        mEtInput.setText(text);

        ta.recycle();

    }

    public InputIArrowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.view_input_arrow, this);


    }

    public void setErrorTip(String error) {
        mErrorTip.setVisibility(VISIBLE);
        mErrorTip.setText(error);
    }

    public String getInputText() {
        return mEtInput.getText().toString();
    }

    public void setText(String text) {
        if (!TextUtils.isEmpty(text)) {
            mErrorTip.setVisibility(GONE);
            mEtInput.setText(text);
        }

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        mErrorTip.setVisibility(GONE);

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
